package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
//import com.sun.org.apache.xml.internal.serializer.utils.MsgKey;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
//import sun.jvm.hotspot.debugger.Page;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 手机验证码发送
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody Map<String, String> map, HttpSession session) {
        String phone = map.get("phone");
        //if (StringUtils.isNotEmpty(phone)){
        //  String code = ValidateCodeUtils.generateValidateCode(4).toString();
        //调用阿里云提供的短信服务完成发送短信
        /*           SMSUtils.sendMessage("瑞吉外卖","",phone,code);*/
        //需要将生成的验证码保存到session
        String code = "1234";
        session.setAttribute(phone, code);
        return R.success("手机验证码发送信息成功");

        //  return R.error("短信发送失败");
    }

    /**
     * 验证码登录
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<String> login(@RequestBody Map<String, String> map, HttpSession session) {
        String phone = map.get("phone");
        String code = map.get("code");
        String codeInSession = (String) session.getAttribute(phone);
        if (codeInSession != null || codeInSession.equals(code)) {
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone, phone);
            User user = userService.getOne(queryWrapper);
            if (user == null) {
                user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
            } else {
                Integer status = user.getStatus();
                if (status == 0) {
                    return R.error("用户已禁用");
                }
            }
            session.removeAttribute(phone);
            session.setAttribute("user", user.getId());
            return R.success("登录成功");
        }
        return R.error("登录失败");
    }

    @PostMapping("/loginout")
    public R<String> logout(HttpSession session){
        session.removeAttribute("user");
        //session.removeAttribute("");
        //退出登录后购物车中的信息需不需要删除 ????????????
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getId,currentId);
        shoppingCartService.remove(queryWrapper);

        return R.success("退出成功");
    }


}